angular.module('starter.controllers')
.controller('CartCtrl', function($scope, museumFac, $stateParams, $timeout, $ionicScrollDelegate, cfg, $location, $cookies, $interval) {
    $scope.height1 = $(window).height() - 110;

    $scope.change_language = function(id){
      $scope.language = id;
      var expireDate = new Date();  
      expireDate.setDate(expireDate.getDate() + 30);                
      $cookies.put('language', id, {'expires': expireDate.toUTCString()});
      location.reload();
      if(id == 0){
        document.getElementById('title').innerHTML = '提示';
        document.getElementById('windowclosebutton').value = '确定';
      }else{
        document.getElementById('title').innerHTML = 'Hint';
        document.getElementById('windowclosebutton').value = 'Confirm';
      }

    }

    $scope.getLanguage = function(){
      $scope.language = $cookies.get('language');
      if($scope.language){
          $scope.language = $scope.language;
      }else{
          $scope.language = 0;
      }
      if($scope.language == 0){
        document.getElementById('title').innerHTML = '提示';
        document.getElementById('windowclosebutton').value = '确定';
      }else{
        document.getElementById('title').innerHTML = 'Hint';
        document.getElementById('windowclosebutton').value = 'Confirm';
      }
    }
    $scope.getLanguage();

    $scope.getCart = function(){
    	museumFac.product.mycart(function(data){
        	$scope.orders = data.info;
          $scope.height2 = $('.cart').height();

          $scope.allprice = 0;
          $scope.allquantity = 0;
          angular.forEach($scope.orders, function(item){
            item.quantity = parseInt(item.quantity, 10);
            if(item.activity_price != 0){
              $scope.allprice = Math.round(($scope.allprice + item.activity_price*item.quantity)*100)/100;
            }else{
              $scope.allprice = Math.round(($scope.allprice + item.price*item.quantity)*100)/100;
            }
            
            $scope.allquantity = $scope.allquantity + item.quantity;
          });
      	},function(data){
        	alert(data.data.info);
    	});
  	}
  	$scope.getCart();

    $scope.checkOpenid = function(){
      museumFac.user.usertest(function(data){
            $scope.is_login = data.info;
          },function(data){
            alert(data.data.info);
        });
    }
    $scope.checkOpenid();

    $scope.add = function(list){
      list.quantity ++;
      museumFac.product.savetocart({
        cartId:list.id,
        quantity:list.quantity
      },function(data){
            $scope.getCart();
          },function(data){
            alert(data.data.info);
      });
    }

    $scope.delete = function(list){
      if(list.quantity >= 1){
        list.quantity --;
      }
      museumFac.product.savetocart({
        cartId:list.id,
        quantity:list.quantity
      },function(data){
            $scope.getCart();
          },function(data){
            alert(data.data.info);
      });
    }

    $scope.fillQuantity = function(list){
      // if(list.quantity == 0||!list.quantity){
      //   alert('数量不能为空');
      //   $scope.cannot = 1;
      //   museumFac.product.savetocart({
      //     cartId:list.id,
      //     quantity:list.quantity
      //   },function(data){
      //       },function(data){
      //         alert(data.data.info);
      //   });
      // }else{
      //     museumFac.product.savetocart({
      //       cartId:list.id,
      //       quantity:list.quantity
      //     },function(data){
      //           $scope.getCart();
      //           $timeout(function() {
      //             $('#product_'+list.id).focus();
      //           }, 500);
      //         },function(data){
      //           alert(data.data.info);
      //     });
        
      // }
      $scope.show_quan = 1;
      $scope.thisquan = list;
    }

    $scope.save1 = function(list){
      museumFac.product.savetocart({
        cartId:list.id,
        quantity:list.quantity
      },function(data){
            $scope.getCart();
          },function(data){
            alert(data.data.info);
      });
      $scope.show_quan = 0;
    }

    $scope.canclesave = function(){
      $scope.show_quan = 0;
    }

    $scope.gotoCheckpay = function(){
      // museumFac.product.cart(function(data){
      //       $scope.orderid = data.info;
      //       window.location.href = cfg.baseUrl + 'home/index/checkpay?orderId=' + $scope.orderid + '/status?status=9';
      //     },function(data){
      //       alert(data.data.info);
      // });

      window.location.href = cfg.baseUrl + 'home/index/checkpay?orderId=0';
    }

})